import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;

// 使用这个类对 DataSource 进行单例模式的封装.
public class DBUtil {
    private volatile static DataSource dataSource = null;
    private DBUtil() { }

    public static DataSource getDataSource() {
        if(dataSource == null) {
            synchronized (DBUtil.class) {
                if (dataSource == null) {
                    dataSource = new MysqlDataSource();
                    ((MysqlDataSource) dataSource).setURL(
                            "jdbc:mysql://127.0.0.1:3306/java105?characterEncoding=utf8&useSSL=false");
                    ((MysqlDataSource) dataSource).setUser("root");
                    ((MysqlDataSource) dataSource).setPassword("1111");
                }
            }
        }
        return dataSource;
    }
}
